using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ProjectCodeS.Models;

namespace ProjectCodeS.Controllers
{   
    public class ClientsController : Controller
    {
		private readonly IStoreRepository storeRepository;
		private readonly IClientRepository clientRepository;

		// If you are using Dependency Injection, you can delete the following constructor
        public ClientsController() : this(new StoreRepository(), new ClientRepository())
        {
        }

        public ClientsController(IStoreRepository storeRepository, IClientRepository clientRepository)
        {
			this.storeRepository = storeRepository;
			this.clientRepository = clientRepository;
        }

        //
        // GET: /Clients/

        public ViewResult Index()
        {
            return View(clientRepository.AllIncluding(client => client.Store, client => client.Tickets));
        }

        //
        // GET: /Clients/Details/5

        public ViewResult Details(int id)
        {
            return View(clientRepository.Find(id));
        }

        //
        // GET: /Clients/Create

        public ActionResult Create()
        {
			ViewBag.PossibleStores = storeRepository.All;
            return View();
        } 

        //
        // POST: /Clients/Create

        [HttpPost]
        public ActionResult Create(Client client)
        {
            if (ModelState.IsValid) {
                clientRepository.InsertOrUpdate(client);
                clientRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleStores = storeRepository.All;
				return View();
			}
        }
        
        //
        // GET: /Clients/Edit/5
 
        public ActionResult Edit(int id)
        {
			ViewBag.PossibleStores = storeRepository.All;
             return View(clientRepository.Find(id));
        }

        //
        // POST: /Clients/Edit/5

        [HttpPost]
        public ActionResult Edit(Client client)
        {
            if (ModelState.IsValid) {
                clientRepository.InsertOrUpdate(client);
                clientRepository.Save();
                return RedirectToAction("Index");
            } else {
				ViewBag.PossibleStores = storeRepository.All;
				return View();
			}
        }

        //
        // GET: /Clients/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View(clientRepository.Find(id));
        }

        //
        // POST: /Clients/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            clientRepository.Delete(id);
            clientRepository.Save();

            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing) {
                storeRepository.Dispose();
                clientRepository.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}

